package com.angel.controller;

import com.angel.bean.Branches;
import com.angel.bean.dto.CountDataDto;
import com.angel.service.BranchesService;
import com.angel.service.CountDataDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/count")
public class countDataDtoController {

    @Autowired
    private CountDataDtoService countDataDtoService;
    @Autowired
    private  BranchesService branchesService;

    private static final String REG_D = "%Y%m%d";
    private static final String REG_M = "%Y%m";
    private static final String REG_Y = "%Y";

    /**
     * demo
     *
     * @return
     */
    @RequestMapping("/getbrach")
    @ResponseBody
    public Map getCountByBranchesIn() {
        Map map = new HashMap();
        map.put("success", false);
        CountDataDto countDataDto = new CountDataDto();
        countDataDto.setRegTime("%Y%m%d");
        countDataDto.setCurrenTime("20180903");
        List<CountDataDto> countDataDtos = countDataDtoService.getCountByBranchesIn(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }

    /**
     * 按照一年中的月份统计数据
     * @return
     */
    @RequestMapping("/getYearPay")
    @ResponseBody
    private Map getYearPay(){
        Map map = new HashMap();
        map.put("success", false);
        CountDataDto countDataDto = new CountDataDto();
        List<CountDataDto> countDataDtos = countDataDtoService.getYearPay(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }



    /**
     * 统计分店充值金额和营业金额
     * @return
     */
    @RequestMapping("/getTurnoverAndPay")
    @ResponseBody
    public Map getBranchPay() {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getCountTurnoverAndPay();
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }


    /**
     * 获取每个分店每天的营业额
     *
     * @return
     */
    @RequestMapping("/getbrachvaluebyD")
    @ResponseBody
    public Map getCountByBranchesValueByeDay() {
        Calendar c = Calendar.getInstance();
        //格式工具
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date startDate = c.getTime();
        Map map = this.getValue(REG_D, sdf.format(c.getTime()));
        return map;
    }

    /**
     * 获取每个分店每月的营业额
     *
     * @return
     */
    @RequestMapping("/getbrachvaluebyM")
    @ResponseBody
    public Map getCountByBranchesValueByMouth() {
        Calendar c = Calendar.getInstance();
        //格式工具
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date startDate = c.getTime();
        Map map = this.getValue(REG_M, sdf.format(c.getTime()));
        return map;
    }

    /**
     * 统计各分店宝宝数量
     *
     * @return
     */
    @RequestMapping("/getCountBaby")
    @ResponseBody
    public Map getCountBaby() {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getCountBaby(null);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }
    /**
     * 各个会卡名称下的用户数量
     * @return
     */
    @RequestMapping("/getVipCardCount")
    @ResponseBody
    public Map  getVipCardCount(){
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getVipCardCount();
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }

    @RequestMapping("/all")
    public String getCount() {
        return "countAll";
    }
    @RequestMapping("/one")
    public String getCountOne() {
        return "countOne";
    }
    @RequestMapping("/branch")
    public String getCountbranch(@RequestParam("br") String br, Model model) {
        Integer brid =null;
            try {
                brid = Integer.valueOf(br);
            }catch (Exception e){
                brid =null;
            }
        if (brid == null){
            return "countAll";
        }else {
            Branches branches = new Branches();
            branches.setBranId(brid);
            branches = branchesService.findOneBranches(branches);
            model.addAttribute("branch",branches);
            model.addAttribute("brid",brid);
        }
        return "countBranch";
    }


    /**
     * t统计分店一月中每一天的营业额
     *
     * @return
     */
    @RequestMapping("/getBrancheAndValueAndEveryDayAndMonth")
    @ResponseBody
    public Map getBrancheAndValueAndEveryDayAndMonth(CountDataDto countDataDto) {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getBrancheAndValueAndEveryDayAndMonth(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }


    /**
     * t统计分店本年度每月的营业额
     *
     * @return
     */

    @RequestMapping("/getBrancheAndValueAndEveryMonthAndYear")
    @ResponseBody
    public Map getBrancheAndValueAndEveryMonthAndYear(CountDataDto countDataDto) {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getBrancheAndValueAndEveryMonthAndYear(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }

    /***
     * 员工开方次数统计
     * @param countDataDto
     * @return
     */
    @RequestMapping("/getCountAndEmp")
    @ResponseBody
    public Map getCountAndEmp(CountDataDto countDataDto) {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getCountAndEmp(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }

    /**
     * 分店月度进货统计，商品和价格以及数量
     * @param countDataDto
     * @return
     */
    @RequestMapping("/getProNameAndCountAndSum")
    @ResponseBody
    public Map getProNameAndCountAndSum(CountDataDto countDataDto) {
        Map map = new HashMap();
        map.put("success", false);
        List<CountDataDto> countDataDtos = countDataDtoService.getProNameAndCountAndSum(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }



    private Map getValue(String regTime, String crrentTime) {
        Map map = new HashMap();
        map.put("success", false);
        CountDataDto countDataDto = new CountDataDto();
        countDataDto.setRegTime(regTime);
        countDataDto.setCurrenTime(crrentTime);
        List<CountDataDto> countDataDtos = countDataDtoService.getCountBranchValue(countDataDto);
        if (countDataDtos != null && countDataDtos.size() > 0) {
            map.put("success", true);
            map.put("data", countDataDtos);
        } else {
            map.put("msg", "信息加载失败");
        }
        return map;
    }
}
